#import "YQPInfoModel.h"

#import "MYMyVC.h"
#import "MYLoginVC.h"
#import "YQNavTitleView.h"
#import "YQMYBaseNavigationController.h"
#import "MYMyNotificationVC.h"
#import "YQOrderManageVC.h"
#import "MYMyUserInfoVC.h"
#import "MYMySetAttentionVC.h"
#import "MYMyCollectVC.h"
#import "MYShopCarVC.h"
#import "YQAddressManageVC.h"
#import "MYMySetVC.h"
#import "MYMyPublishVC.h"
#import "MYMyCommentVC.h"
#import "MYCreditsMallVC.h"
#import "MYSheardPaymentVC.h"
#import "MYDiscountCouponVC.h"
#import "MYConnectionUsVC.h"
#import "YQRefundOrderListVC.h"
@interface MYMyVC ()<MYMyHeaderViewDelegate,MYMyOtherActionCellDelegate>
@property (nonatomic, strong) UIButton * notiBtn;
@property (nonatomic, strong) UIButton * QRCodeBtn;

@property (nonatomic, strong) UIView *redView;

@property (strong, nonatomic) YQPInfoModel *model;
@end

@implementation MYMyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 一行代码搞定导航栏颜色
    [self wr_setNavBarBarTintColor:kAPPThemeColor];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getUserInfoApi];
}

#pragma mark - 查询个人资料
- (void)getUserInfoApi
{
    [YQPNetWorkingManager sendPostRequest:kPOST_PROFILE_URL parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        self.model = [YQPInfoModel mj_objectWithKeyValues:responseObject.data];
        [self.listTableView reloadData];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

-(void)setupView {
    [super setupView];
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = kAPPThemeColor;
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    _notiBtn = [UIButton buttonWithType:0];
    [_notiBtn setImage:YQIMG(@"MyVC_Nav_Right_Msg_icon") forState:(UIControlStateNormal)];
    [bgView addSubview:self.notiBtn];
    [self.notiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-12);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    
    [self.notiBtn addSubview:self.redView];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.notiBtn.mas_top).mas_equalTo(4);
        make.right.mas_equalTo(self.notiBtn.mas_right).mas_equalTo(-3);
        make.size.mas_equalTo(CGSizeMake(6, 6));
    }];
    
    _QRCodeBtn = [UIButton buttonWithType:0];
    [_QRCodeBtn setImage:YQIMG(@"MyVC_Nav_Right_code_icon") forState:(UIControlStateNormal)];
    [bgView addSubview:self.QRCodeBtn];
    self.QRCodeBtn.hidden = YES;
    [self.QRCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(self.notiBtn.mas_left).mas_equalTo(-8);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    
    [self.notiBtn addTarget:self action:@selector(notiAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.QRCodeBtn addTarget:self action:@selector(scanAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea-Height_TabBar);
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYMyHeaderView");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMyOtherActionCell")
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYMyOtherActionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMyOtherActionCell"];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 205;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 26;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYMyHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYMyHeaderView"];
    [headerView setModel:self.model];
    YQPLoginModel *account = [YQPUserTool account];
    headerView.userName.text = self.model.name?:account.userName?:account.mobile;
    [headerView.userIcon sd_setImageWithURL:[NSURL URLWithString:self.model.headImg?:account.userHead] placeholderImage:IMAGE_NAMED(@"")];
    headerView.vipLb.text = [NSString stringWithFormat:@"Vip: %@",self.model.vipLevel.length >0 ?self.model.vipLevel:@"0"];
    headerView.integralLb.text = [NSString stringWithFormat:@"%@: %@",kYQLS(@"积分"),self.model.integral.length >0 ?self.model.integral:@"0"];
    
    headerView.delegate = self;
    [headerView.userInfoBgView addTapAction:^(id  _Nonnull sender) {
        //跳转到个人资料
        MYMyUserInfoVC *infoVC = [[MYMyUserInfoVC alloc] init];
        kYQPUSH(infoVC);
    }];
    [headerView.meOrderBgView addTapAction:^(id  _Nonnull sender) {
        //我的订单
        YQOrderManageVC *orderVC = [[YQOrderManageVC alloc] init];
        orderVC.type = 0;
        kYQPUSH(orderVC);
    }];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 323;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
-(void)notiAction:(UIButton *)sender {
    MYMyNotificationVC *notificationVC = [[MYMyNotificationVC alloc] init];
    kYQPUSH(notificationVC);
}
-(void)scanAction:(UIButton *)sender {
    
}
#pragma mark -- lazy
-(UIView *)redView {
    if (!_redView) {
        _redView = [[UIView alloc] init];
        _redView.backgroundColor = HexColor(0x79C4C7);
        _redView.layer.cornerRadius = 3;
        _redView.clipsToBounds = true;
    }
    return _redView;
}

#pragma mark -- 个人信息 关注、粉丝、评论、收藏

#pragma mark -- MYMyHeaderViewDelegate
- (void)showCountSelectAction:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        MYMySetAttentionVC *attentionVC = [[MYMySetAttentionVC alloc] init];
        attentionVC.type = YQPUserFocus;
        attentionVC.model = self.model;
        kYQPUSH(attentionVC);
    }else if (indexPath.item == 1) {
        MYMySetAttentionVC *attentionVC = [[MYMySetAttentionVC alloc] init];
        attentionVC.type = YQPUserFans;
        attentionVC.model = self.model;

        kYQPUSH(attentionVC);
    }else if (indexPath.item == 2) {
        MYMyCommentVC *commentVC = [[MYMyCommentVC alloc] init];
        kYQPUSH(commentVC);
    }else{
        MYMyCollectVC *collectVC = [[MYMyCollectVC alloc] init];
        kYQPUSH(collectVC);
    }
}

#pragma mark - 我的订单
-(void)orderSelectAction:(NSIndexPath *)indexPath {
    if (indexPath.item == 4) {
        YQRefundOrderListVC *refundVC = [YQRefundOrderListVC new];
        kYQPUSH(refundVC);
    }else{
        //我的订单
        YQOrderManageVC *orderVC = [[YQOrderManageVC alloc] init];
        orderVC.type = indexPath.item+1;
        kYQPUSH(orderVC);
    }
}
#pragma mark -- MYMyHeaderViewDelegate end
#pragma mark -- MYMyOtherActionCellDelegate 其他功能
-(void)otherSelectAction:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        MYMyPublishVC *publishVC = [[MYMyPublishVC alloc] init];
        kYQPUSH(publishVC);
    }else if(indexPath.item == 1){
        MYSheardPaymentVC *paymentVC = [MYSheardPaymentVC new];
        kYQPUSH(paymentVC);
//        [MBProgressHUD showMessage:kYQLS(@"暂未开放") toView:self.view];
    }else if(indexPath.item == 2){
        MYDiscountCouponVC *discountCouponVC = [[MYDiscountCouponVC alloc] init];
        kYQPUSH(discountCouponVC);
    }else if(indexPath.item == 3){
        MYShopCarVC *carVC = [MYShopCarVC new];
        kYQPUSH(carVC);
    }else if(indexPath.item == 4){
        YQAddressManageVC *addVC = [YQAddressManageVC new];
        kYQPUSH(addVC);
    }else if(indexPath.item == 5){
        MYCreditsMallVC *mallVC = [[MYCreditsMallVC alloc] init];
        kYQPUSH(mallVC);
    }else if(indexPath.item == 6){
        MYConnectionUsVC * connectionUsVC = [[MYConnectionUsVC alloc] init];
        kYQPUSH(connectionUsVC);
    }else if(indexPath.item == 7){
        MYMySetVC *setVC = [MYMySetVC new];
        setVC.model = self.model;
        kYQPUSH(setVC);
    }
}
#pragma mark -- MYMyOtherActionCellDelegate end
@end
